package com.flower.controller;


import com.flower.model.entity.Foster;
import com.flower.result.PageResult;
import com.flower.result.Result;
import com.flower.service.IFosterService;
import com.github.pagehelper.Page;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lm
 * @create 2022-03-17 16:29
 */
@RestController
@RequestMapping("/foster")
public class FosterController {

    @Autowired
    private IFosterService fosterService;


    //新增和修改
    @PostMapping
    public Result save(@RequestBody Foster foster) {
        fosterService.saveOrUpdate(foster);
        //新增或者更新
        return Result.success("操作成功");

    }

    //查询所有数据
    @GetMapping("/")
    public Result findAll() {
        return Result.success(fosterService.list());
    }

    //删除数据
    @DeleteMapping("/{id}")

    public Result delete(@PathVariable Integer id) {
        fosterService.removeById(id);
        return Result.success("删除成功");
    }

    //批量删除
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        fosterService.removeByIds(ids);
        return Result.success("批量删除成功");
    }

    //分页查询——mybatis-plus的方式查询
    @GetMapping("/page")
    public Result<PageResult> findPage(@RequestParam(defaultValue = "") String phone,
                                       @RequestParam(defaultValue = "") String fosName,
                                       @RequestParam String startTime,
                                       @RequestParam(defaultValue = "") String fosOper,
                                       @RequestParam(defaultValue = "") String fosStatus,
                                       @RequestParam Integer pageNum,
                                       @RequestParam Integer pageSize
    ) {

        Page<Foster> page = fosterService.findPage(new Page<>(pageNum, pageSize), phone, fosName, startTime, fosOper,
                fosStatus);

        return Result.success(new PageResult(page.getTotal(), page.getResult()));
    }
}






